
package com.contractmodule.dao;
import com.contractmodule.bean.TbBackSection;

import java.sql.*;
import java.io.*;
import java.util.*;
import util.*;
public class TbBackSectionDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbBackSection tbBackSection=null;
    private List<TbBackSection> tbBackSectionList=null;

    public boolean add(TbBackSection inTbBackSection){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_BackSection(Responsible,BackSectionFlowInfo,CompletionRates,ApplicationAmount,DebitAmount,FinalAmount,ReturnedAmount,Balance,ExpectedBackSectionTime,BackSectionTime,UserId) values(?,?,?,?,?,?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbBackSection.getResponsible());
        pt.setString(2,inTbBackSection.getBackSectionFlowInfo());
        pt.setDouble(3,new Double(inTbBackSection.getCompletionRates()));
        pt.setDouble(4,new Double(inTbBackSection.getApplicationAmount()));
        pt.setDouble(5,new Double(inTbBackSection.getDebitAmount()));
        pt.setDouble(6,new Double(inTbBackSection.getFinalAmount()));
        pt.setDouble(7,new Double(inTbBackSection.getReturnedAmount()));
        pt.setDouble(8,new Double(inTbBackSection.getBalance()));
        pt.setString(9,inTbBackSection.getExpectedBackSectionTime());
        pt.setString(10,inTbBackSection.getBackSectionTime());
        pt.setInt(11,inTbBackSection.getUserId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public boolean del(int inBackSectionId){
        return UtilDAO.del("Tb_BackSection", "BackSectionId", inBackSectionId);
    }
    public boolean edit(TbBackSection inTbBackSection){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_BackSection set Responsible=?,BackSectionFlowInfo=?,CompletionRates=?,ApplicationAmount=?,DebitAmount=?,FinalAmount=?,ReturnedAmount=?,Balance=?,ExpectedBackSectionTime=?,BackSectionTime=?,UserId=?,CreateTime=getdate() where BackSectionId=?";
        pt = conn.prepareStatement(sql);
      
        pt.setString(1,inTbBackSection.getResponsible());
        pt.setString(2,inTbBackSection.getBackSectionFlowInfo());
        pt.setDouble(3,new Double(inTbBackSection.getCompletionRates()));
        pt.setDouble(4,new Double(inTbBackSection.getApplicationAmount()));
        pt.setDouble(5,new Double(inTbBackSection.getDebitAmount()));
        pt.setDouble(6,new Double(inTbBackSection.getFinalAmount()));
        pt.setDouble(7,new Double(inTbBackSection.getReturnedAmount()));
        pt.setDouble(8,new Double(inTbBackSection.getBalance()));
        pt.setString(9,inTbBackSection.getExpectedBackSectionTime());
        pt.setString(10,inTbBackSection.getBackSectionTime());
        pt.setInt(11,inTbBackSection.getUserId());
        pt.setInt(12,inTbBackSection.getBackSectionId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public TbBackSection getTbBackSectionByBackSectionId(int inBackSectionId) {
        tbBackSection=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_BackSection where BackSectionId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inBackSectionId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbBackSection == null)tbBackSection = getTbBackSection(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbBackSection;
    }
    public List<TbBackSection> getTbBackSectionAll() {
        tbBackSectionList=new ArrayList<TbBackSection>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_BackSection";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbBackSectionList.add(getTbBackSection(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbBackSectionList;
    }
    /**
     * get the backSections by backSectionIds
     *@author qingyu zhang
     *@function:
     * @param backSectionId
     * @return
     *2011-6-19
     *上午10:37:47
     *ProjectManagerT
     *com.backsectionmodule.dao
     *List<TbBackSection>
     */
    public List<TbBackSection> getTbBackSectionAll(String backSectionId) {
    	tbBackSectionList=new ArrayList<TbBackSection>();
    	try{
    		conn = ConnectDB.getSqlServerConnection();
    		sql = "select * from Tb_BackSection where BackSectionId in("+backSectionId+")";
    		pt = conn.prepareStatement(sql);
    		rs = pt.executeQuery();
    		while (rs.next()) {
    			tbBackSectionList.add(getTbBackSection(rs));
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally {
    		ConnectDB.closeSqlConnection();        }
    	return tbBackSectionList;
    }
    public List<TbBackSection> getTbBackSectionAll(VskPage vp) {
        tbBackSectionList=new ArrayList<TbBackSection>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_BackSection";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_BackSection", "BackSectionId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbBackSectionList.add(getTbBackSection(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbBackSectionList;
    }
    public TbBackSection getTbBackSection(ResultSet rs) throws Exception{
        TbBackSection tbBackSection = new TbBackSection();
        tbBackSection.setBackSectionId(rs.getInt("BackSectionId"));
        tbBackSection.setResponsible(rs.getString("Responsible"));
        tbBackSection.setBackSectionFlowInfo(rs.getString("BackSectionFlowInfo"));
        tbBackSection.setCompletionRates(rs.getString("CompletionRates"));
        tbBackSection.setApplicationAmount(rs.getString("ApplicationAmount"));
        tbBackSection.setDebitAmount(rs.getString("DebitAmount"));
        tbBackSection.setFinalAmount(rs.getString("FinalAmount"));
        tbBackSection.setReturnedAmount(rs.getString("ReturnedAmount"));
        tbBackSection.setBalance(rs.getString("Balance"));
        tbBackSection.setExpectedBackSectionTime(rs.getString("ExpectedBackSectionTime"));
        tbBackSection.setBackSectionTime(rs.getString("BackSectionTime"));
        tbBackSection.setUserId(rs.getInt("UserId"));
        tbBackSection.setCreateTime(rs.getString("CreateTime"));
        return tbBackSection;
    }
    
}
